package com.puppet.building182.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.puppet.building182.pojo.DTO.HouseDto;
import com.puppet.building182.pojo.House;
import com.puppet.building182.pojo.Result;
import com.puppet.building182.pojo.enums.BuildingCode;
import com.puppet.building182.service.HouseService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/house")
public class HouseController {
    @Autowired
    private HouseService houseService;


    //@GetMapping
//public Result<?> countHouseByBuildingId(Integer id) {
//    return Result.ok(houseService.countHouseByBuildingId(id));
//}
    @GetMapping("/getHouseIdAnd")
    public Result<?> getHouseIdAnd() {
        return Result.ok(houseService.getHouseIdAnd());
    }

    @PostMapping("/{pageNum}/{pageSize}")
    public Result<?> getHouse(@PathVariable("pageNum") Integer pageNum,
                              @PathVariable("pageSize") Integer pageSize,
                              @RequestBody(required = false) HouseDto houseDto) {
        PageHelper.startPage(pageNum, pageSize);
        List<House> list = houseService.getHouse(houseDto);
        PageInfo<House> pageInfo = new PageInfo<>(list);
        return Result.ok(pageInfo);
    }

    @GetMapping("/getHouseById/{id}")
    public Result<?> getHouseById(@PathVariable("id") Integer id) {
        return Result.ok(houseService.getHouseById(id));
    }

    @PostMapping
    public Result<?> addHouse(@RequestBody House house) {
        return Result.ok(houseService.addHouse(house), BuildingCode.SAVE_SUCCESS.getMessage());
    }

    @DeleteMapping("/deleteById/{id}")
    public Result<?> deleteById(@PathVariable("id") Integer id) {
        return Result.ok(houseService.deleteById(id),BuildingCode.DELETE_SUCCESS.getMessage());
    }

    @PostMapping("/deleteHouse")
    public Result<?> deleteHouse(@RequestBody List<Integer> house) {
        return Result.ok(houseService.deleteHouse(house));
    }

    @GetMapping("/getState")
    public Result<?> getState() {
        return Result.ok(houseService.getState());
    }
    @GetMapping("download")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("房屋数据", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), House.class).sheet("模板").doWrite(houseService.getHouse(new HouseDto()));
    }


    @PutMapping("/updateHouse")
    public Result<?> updateHouse(@RequestBody House house) {
        return Result.ok(houseService.updateHouse(house),BuildingCode.SAVE_SUCCESS.getMessage());
    }

}
